#include "../include/head.h"

//管理员登录
int admin_login() {
    char username[20];
    char password[20];
    int attempts = 0;

    while (attempts < 3) {
        printf("请输入管理员"GREEN("用户名")": ");
        scanf("%s", username);
        printf("请输入管理员"GREEN("密码")": ");
        scanf("%s", password);

        if (strcmp(username, ADM) == 0 && strcmp(password, PWD) == 0) {
            return 1; // 登录成功
        } else {
            printf(RED("用户名或密码错误，请重新输入。")"\n");
            attempts++;
        }
    }

    printf("您已超过最大尝试次数。\n");
    return 0; // 登录失败
}

//用户登录
int user_login(UserList *list) {
    char password[20];
    int attempts = 0;

    while (attempts < 3) {
        printf("请输入"GREEN("用户名")": ");
        scanf("%s", username);
        printf("请输入"GREEN("密码")": ");
        scanf("%s", password);

        UserNode *current = list->head;
        while (current != NULL) {
            if (strcmp(current->user.username, username) == 0 && strcmp(current->user.password, password) == 0) {
                return 1; // 登录成功
            }
            current = current->next;
        }

        printf(RED("用户名或密码错误，请重新输入。")"\n");
        attempts++;
    }

    printf("您已超过最大尝试次数。\n");
    return 0; // 登录失败
}

//用户注册
void user_register(UserList *list) {
    User user;
    system("clear");
    printf("请输入"GREEN("用户名")": ");
    scanf("%s", user.username);
    printf("请输入"GREEN("密码")": ");
    scanf("%s", user.password);

    // 检查用户名是否已存在
    UserNode *current = list->head;
    while (current != NULL) {
        if (strcmp(current->user.username, user.username) == 0) {
            printf(RED("用户名已存在，请重新注册。")"\n");
            return;
        }
        current = current->next;
    }
    // 添加新用户
    add_user(list, user);
    // 将新用户信息写入文件
    write_user_to_file(list); 
    printf("注册成功！\n");
}